from pydantic import BaseModel, validator
from typing import Optional, List
from app.models.models import SenderEnum, ContentTypeEnum

class SendMessageRequest(BaseModel):
    conversation_id: Optional[int] = None
    sender: SenderEnum
    content_type: ContentTypeEnum
    content: Optional[str] = None      # 文本内容
    oss_urls: Optional[List[str]] = None  # 多张图片URL列表
    conversation_name: Optional[str] = None

    @validator('oss_urls')
    def validate_oss_urls(cls, v):
        if v is not None and not isinstance(v, list):
            raise ValueError('oss_urls必须是字符串列表')
        return v

class StoreMessageRequest(BaseModel):
    conversation_id: Optional[int] = None
    sender: SenderEnum
    content_type: ContentTypeEnum
    content: Optional[str] = None      # 文本内容
    oss_urls: Optional[List[str]] = None  # 多张图片URL列表
    base64_images: Optional[List[str]] = None  # base64格式的图片列表

    @validator('oss_urls')
    def validate_oss_urls(cls, v):
        if v is not None and not isinstance(v, list):
            raise ValueError('oss_urls必须是字符串列表')
        return v

    @validator('base64_images')
    def validate_base64_images(cls, v):
        if v is not None:
            if not isinstance(v, list):
                raise ValueError('base64_images必须是字符串列表')
            for i, img in enumerate(v):
                if not isinstance(img, str):
                    raise ValueError(f'base64_images[{i}]必须是字符串')
                # 更宽松的验证：允许纯base64或data URL格式
                if not (img.startswith('data:image/') or len(img) > 100):
                    raise ValueError(f'base64_images[{i}]必须是有效的base64图片格式')
        return v

class MessageResponse(BaseModel):
    id: int
    sender: SenderEnum
    content_type: ContentTypeEnum
    content: Optional[str] = None      # 文本内容
    oss_urls: Optional[List[str]] = None  # 多张图片URL列表
    created_at: str 